package com.example.actearly_revised;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Information holder class to hold the account information of the user.
 *
 */
public class User {
	protected String name;
	protected String email;
	protected String childname;
	protected String Birthdate;
	protected String doctor;
	protected int gender;
	protected int childId;
	protected String docemail;
		

	public User( String name, String email, String childname, String birthdate, String doctor, int gender, String docemail, int childId ) {
		this.name = name;
		this.email = email;
		this.childname = childname;
		this.Birthdate = birthdate;
		this.doctor = doctor;
		this.gender = gender;
		this.docemail = docemail;
		this.childId = childId;
	}
	
	public String getDocEmail(){
		return docemail;
	}
	
	public void setDocEmail(String e){
		this.docemail = e;
	}
	
	public int getChildId(){
		return childId;
	}
	
	public int getGender(){
		return gender;
	}
	
	public void setGender(int g){
		this.gender = g;
	}
		
	public String getDoctor() {
		return doctor;
	}

	public void setDoctor(String doctor) {
		this.doctor = doctor;
	}
	
	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getChildname()
	{
		return childname;
	}

	public void setUsername( String Childname)
	{
		this.childname = Childname;
	}

	public String getBirthdate()
	{
		return Birthdate;
	}

	public void setBirthdate(String birthdate)
	{
		this.Birthdate = birthdate;
	}

	public String getAge(String birthdate)
	{
		// get current date/birthdate, break into day/month/year
		int year= Integer.parseInt(Birthdate.substring(6,10));
		int month= Integer.parseInt(Birthdate.substring(0,2));
		month=month-1;
		int day= Integer.parseInt(Birthdate.substring(3,5));
		int ageYears=0;
		int ageMonths=0;
		int ageDays=0;
		int totalMonths=0;
		String age= "";

		//new calendar for current date, birth date
		Calendar cd = Calendar.getInstance();
		System.out.println(cd);
		Calendar bd = new GregorianCalendar(year, month, day);

		//calculate age with subtraction of years/months, adjust as needed
		// months here are from 0-11 for some reason, hence the +1
		ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
		if(cd.before(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
			ageYears--;
			ageMonths = (12 - (bd.get(Calendar.MONTH)+1 )) + (bd.get(Calendar.MONTH));
			if(day > cd.get(Calendar.DAY_OF_MONTH)){
				ageDays = day - cd.get(Calendar.DAY_OF_MONTH);
			}
			else if(day < cd.get(Calendar.DAY_OF_MONTH)){
				ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
			}
			else{
				ageDays = 0;
			}
		}
		else if(cd.after(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
			ageMonths = (cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)));
			if(day > cd.get(Calendar.DAY_OF_MONTH))
				ageDays = day - cd.get(Calendar.DAY_OF_MONTH) - day;
			else if(day < cd.get(Calendar.DAY_OF_MONTH)){
				ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
			}
			else
				ageDays = 0;
		}
		else{
			ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
			ageMonths = 0;
			ageDays = 0;
		}
		// Milestones are in months before 2 years old, in years after.
		// So check their age and display accordingly
		totalMonths= 12*(ageYears)+ageMonths;

		if (totalMonths<24)
		{
			if (totalMonths>1)
			{
				age= Integer.toString(totalMonths)+ " months old.";
			}
			else if (totalMonths==1)
			{
				age= "1 month old.";
			}
			else 
			{
				age=  " less than 1 month old.";
			}
		}
		else if (totalMonths>=24)
		{
			String yrString= "";
			String monthString= "";
			if (ageYears==1)
			{ yrString= "1 year";
			}
			else 
			{ yrString= Integer.toString(ageYears)+" years";
			}

			if (ageMonths==0)
			{ monthString=" old.";
			}
			else if (ageMonths==1)
			{
				monthString= ", 1 month old.";
			}
			else
			{
				monthString= ", "+ Integer.toString(ageMonths)+ " months old.";
			}

			age= yrString+monthString;
		}
		return age;
	}
}

